package com.wx.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * <p>Title:	  Wrapper. </p>
 * <p>Description 包装类 </p>
 * <p>Company:    医百科技有限公司 </p>
 *
 * @Author <a href="xinglinglove1029@163.com"/>杨文生</a>
 * @CreateDate 2017/4/19 10:58
 */
@ApiModel(value = "Wrapper")
public class Wrapper<T> implements Serializable {

    private static final long serialVersionUID = 8276555307541965853L;

    /**
     * 成功码.
     */
    public static final int SUCCESS_CODE = 200;

    /**
     * 成功信息.
     */
    public static final String SUCCESS_MESSAGE = "操作成功";

    /**
     * 错误码.
     */
    public static final int ERROR_CODE = 500;

    /**
     * 错误信息.
     */
    public static final String ERROR_MESSAGE = "服务内部异常";

    /**
     * 编号.
     */
    @ApiModelProperty(value = "业务状态码")
    private int code;

    /**
     * 信息.
     */
    @ApiModelProperty(value = "消息")
    private String msg;

    /**
     * 结果数据
     */
    @ApiModelProperty(value = "结果数据")
    private T data;

    /**
     * Instantiates a new wrapper. default code=200
     */
    public Wrapper() {
        this(SUCCESS_CODE, SUCCESS_MESSAGE);
    }

    /**
     * Instantiates a new wrapper.
     *
     * @param code    the code
     * @param msg the msg
     */
    public Wrapper(int code, String msg) {
        this.code(code).msg(msg);
    }

    /**
     * Instantiates a new wrapper.
     *
     * @param code    the code
     * @param msg the msg
     * @param result  the result
     */
    public Wrapper(int code, String msg, T result) {
        super();
        this.code(code).msg(msg).data(result);
    }

    /**
     * Gets the 编号.
     *
     * @return the 编号
     */
    public int getCode() {
        return code;
    }

    /**
     * Sets the 编号.
     *
     * @param code the new 编号
     */
    public void setCode(int code) {
        this.code = code;
    }

    /**
     * Gets the 信息.
     *
     * @return the 信息
     */
    public String getMsg() {
        return msg;
    }

    /**
     * Sets the 信息.
     *
     * @param msg the new 信息
     */
    public void setMsg(String msg) {
        this.msg = msg;
    }

    /**
     * Gets the 结果数据.
     *
     * @return the 结果数据
     */
    public T getData() {
        return data;
    }

    /**
     * Sets the 结果数据.
     *
     * @param data the new 结果数据
     */
    public void setData(T data) {
        this.data = data;
    }

    /**
     * Sets the 编号 ，返回自身的引用.
     *
     * @param code the new 编号
     * @return the wrapper
     */
    public Wrapper<T> code(int code) {
        this.setCode(code);
        return this;
    }

    /**
     * Sets the 信息 ，返回自身的引用.
     *
     * @param msg the new 信息
     * @return the wrapper
     */
    public Wrapper<T> msg(String msg) {
        this.setMsg(msg);
        return this;
    }

    /**
     * Sets the 结果数据 ，返回自身的引用.
     *
     * @param data the new 结果数据
     * @return the wrapper
     */
    public Wrapper<T> data(T data) {
        this.setData(data);
        return this;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return this.getCode() == Wrapper.SUCCESS_CODE;
    }

    @JsonIgnore
    public boolean isFail() {
        return !isSuccess();
    }


}
